import {dataRegionUtil} from "@/plugins/pageoffice/dataRegion.js";
import pageofficeCtrlUtil, {callbackPOParentFunction} from "@/plugins/pageoffice/pageoffice-word-excel.js";
import {dateFormat} from "@/utils/ruoyi.js";
import useLoginUserStore from "@/store/modules/user.js";
import {setCurrentIsRefreshAdvice} from "@/utils/document.js";

export function approvalAdviceFunctions(adviceDialog, advice, adviceDataRegion) {
    const loginUserStore = useLoginUserStore()
    //注意：此处定义的印章名称变量值，需与电子签章简易平台中的印章名称保持一致才能调用，印章名称默认格式：真实姓名+签名章
    const sealName = loginUserStore.nickName + '签名章'

    //根据签章人删除印章（李志）
    const deleteSeal = () => {
        let sealsJson = pageofficectrl.zoomseal.SealsAsJson
        if (sealsJson) {
            let sealList = JSON.parse(sealsJson)
            let sealTime, sealName
            for(let key in sealList ){
                sealTime = sealList[key].signtime
                sealName = sealList[key].name
                if(sealTime === advice.signTime) {
                    pageofficectrl.zoomseal.DeleteByName(sealName)
                }
            }
        }
    }

    const handleDeleteAdvice = () => {
        pageofficectrl.word.SetValueToDataRegion(adviceDataRegion.dataRegionNameForAdvice, advice.initMessage)
        pageofficectrl.word.SetValueToDataRegion(adviceDataRegion.dataRegionNameForOperateUserName, advice.initOperateUserName)
        pageofficectrl.word.SetValueToDataRegion(adviceDataRegion.dataRegionNameForOperateDate, advice.initOperateDate)
        advice.message = ''
        if (adviceDataRegion.dataRegionNameForAddSeal) {
            deleteSeal()
        }
    }

    const locateAndSeal = (dataRegionNameForAddSeal) => {
        if (advice.signTime) {
            deleteSeal()
        }
        dataRegionUtil.locateDataRegion(dataRegionNameForAddSeal)
        //折叠光标，定位到选中数据区域的结束位置
        pageofficectrl.word.SelectionCollapse(1)
        //在光标位置加载印章
        pageofficectrl.zoomseal.AddSealByName(sealName, false, "$cursor$")
        let sealsJson = pageofficectrl.zoomseal.SealsAsJson
        let sealList = JSON.parse(sealsJson)
        advice.signTime = sealList[sealList.length - 1].signtime
    }

    const displayAdvice = (adviceDataRegion, advice) => {
        //将form表单的值回填到word中
        pageofficectrl.word.SetValueToDataRegion(adviceDataRegion.dataRegionNameForAdvice, advice.message)
        pageofficectrl.word.SetValueToDataRegion(adviceDataRegion.dataRegionNameForOperateUserName, loginUserStore.nickName)
        pageofficectrl.word.SetValueToDataRegion(adviceDataRegion.dataRegionNameForOperateDate, dateFormat(new Date(), false))
    }

    const handleAdviceDialogOpen = () => {
        // 当对话框打开时执行的逻辑
        pageofficectrl.Enabled = false
        adviceDialog.isOpen = true
    }

    const handleCloseAdvice = () => {
        // 当对话框关闭时执行的逻辑
        pageofficectrl.Enabled = true
        adviceDialog.isOpen = false
    }

    const handleSubmitAdvice = (form) => {
        advice.message = form.message
        displayAdvice(adviceDataRegion, advice)
        handleCloseAdvice()
        if (adviceDataRegion.dataRegionNameForAddSeal) {
            locateAndSeal(adviceDataRegion.dataRegionNameForAddSeal)
        }
    }

    return {
        handleAdviceDialogOpen,
        handleCloseAdvice,
        handleSubmitAdvice,
        handleDeleteAdvice
    }
}

export function approvalFunctions() {
    const saveApprovalFile = (docId, advice, signTime) => {
        //处理可能包含中文的参数
        let jsonParams = {id: docId, advice: advice, signTime: signTime}
        let params = new URLSearchParams(jsonParams).toString()
        //设置保存地址
        pageofficectrl.SaveFilePage = `/doc/process/updateApprovalWord?${params}`
        //执行保存
        pageofficectrl.WebSave()
        //服务端返回保存结果
        let strCustomSaveResult = pageofficectrl.CustomSaveResult
        if ('ok' === strCustomSaveResult) {
            callbackPOParentFunction(null, 'refreshAdviceList', () => {
                pageofficeCtrlUtil.msgSuccess('保存成功')
            })
        }
    }

    return {
        saveApprovalFile
    }
}

export function refreshAdviceListFunction(currentIsRefreshAdvice) {
    onMounted(()=> {
        //此行必须放在onMounted事件中，否则pageoffice无法回调
        window.POPageMounted={refreshAdviceList}
    })

    //呈批表页面回调刷新意见列表
    function refreshAdviceList() {
        setCurrentIsRefreshAdvice(currentIsRefreshAdvice)
        return 'ok'
    }

    return {
        refreshAdviceList
    }
}
